package com.jingdong.dao.address.impl;


import com.jingdong.dao.address.ProvinceDao;
import com.jingdong.model.Province;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProvinceDaoImpl implements ProvinceDao {
    public List<Province> getProvinceList() {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            List<Province> list=sqlSession.selectList("com.landeqing.model.provinceModel.getProvinceList");
            return  list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public Province getProvinceById(int id) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            Province province=sqlSession.selectOne("com.landeqing.model.provinceModel.getProvinceById",id);
            return province;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public int save(Province province) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            int result=sqlSession.insert("com.landeqing.model.provinceModel.save",province);
            sqlSession.commit();
            sqlSession.close();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
    //根据省的名字查询省的id
    public Province getProvinceForLikeByName(String provinceName) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
           List<Province> provinceList= sqlSession.selectList("com.landeqing.model.provinceModel.getProvinceForLikeByName",provinceName);
           if(provinceList!=null && provinceList.size()>0){
               return provinceList.get(0);
           }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
